#include<stdio.h>
#define ch1 ch
//这题不会做
int main(void)
{
    int count;scanf("%d",&count);
    int n = 0;
    for (int i = 0; i < count; i++)
    {
        char ch[10];scanf("%s",ch);

        while (ch[n] != '\0'){n++;}

        int boo = 1;
        while (boo)
        {
            boo = 0;
            for (int j = 0; j < n-1; j++)
            {
                char temp;
                if (ch[j]>ch[j+1])
                {
                    temp = ch[j];
                    ch[j] = ch[j+1];
                    ch[j+1] = temp;
                    boo = 1;
                }
                
            }
            
        }
        


        int m = 1;
        for (int k = 1; k < n; k++)
        {
            m*=k;
        }
        
        if (n == 2)
        {
            printf("%s\n",ch);
            printf("%c%c\n",ch[1],ch1[0]);
        }
        if (n == 3)
        {
            printf("%s\n",ch);
            printf("%c%c%c\n",ch1[0],ch1[2],ch1[1]);
            printf("%c%c%c\n",ch1[1],ch1[0],ch1[2]);
            printf("%c%c%c\n",ch1[1],ch1[2],ch1[0]);
            printf("%c%c%c\n",ch1[2],ch1[0],ch1[1]);
            printf("%c%c%c\n",ch1[2],ch1[1],ch1[0]);
        }
        
        printf("\n");

    }
    
    
}